package cn.danger.com.view.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;

import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.datatype.BmobDate;
import cn.danger.com.activity.R;
import cn.danger.com.model.entity.Schedule;
import cn.danger.com.presenter.SchedulePresenter;
import cn.danger.com.util.FileCacheService;

/**
 * Created by 俊 on 2017/1/5.
 */
public class CalendarScheduleAdapter extends BaseSwipeAdapter {

    private Context context;
    private List<Schedule> list;
    private SchedulePresenter presenter;
    private int work_color;
    private int route_color;
    private int life_color;

    public CalendarScheduleAdapter(Context context, List<Schedule> list, SchedulePresenter presenter) {
        this.context = context;
        this.list = list;
        this.presenter = presenter;
        initColor(context);

    }

    private void initColor(Context context) {
        int[] colors = new int[12];
        colors[0] = context.getResources().getColor(R.color.color1);
        colors[1] = context.getResources().getColor(R.color.color2);
        colors[2] = context.getResources().getColor(R.color.color3);
        colors[3] = context.getResources().getColor(R.color.color4);
        colors[4] = context.getResources().getColor(R.color.color5);
        colors[5] = context.getResources().getColor(R.color.color6);
        colors[6] = context.getResources().getColor(R.color.color7);
        colors[7] = context.getResources().getColor(R.color.color8);
        colors[8] = context.getResources().getColor(R.color.color9);
        colors[9] = context.getResources().getColor(R.color.color10);
        colors[10] = context.getResources().getColor(R.color.color11);
        colors[11] = context.getResources().getColor(R.color.color12);
        Map<String, Object> map = (Map<String, Object>) new FileCacheService(context).getSharePreference("setting");
        work_color = colors[(int) map.get("work_color") - 1];
        route_color = colors[(int) map.get("route_color") - 1];
        life_color = colors[(int) map.get("life_color") - 1];

    }

    @Override
    public int getSwipeLayoutResourceId(int i) {
        return R.id.swipe;
    }

    @Override
    public View generateView(final int position, ViewGroup parent) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.calendar_listview_item, parent, false);
        final SwipeLayout swipeLayout = (SwipeLayout) convertView.findViewById(getSwipeLayoutResourceId(position));
        convertView.findViewById(R.id.delete_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.deleteSchedule(list.get(position));
                swipeLayout.close();
            }
        });
        return convertView;
    }

    @Override
    public void fillValues(int i, View convertView) {
        View view = convertView.findViewById(R.id.view);
        TextView content_textView = (TextView) convertView.findViewById(R.id.content_textView);
        TextView date_start_textView = (TextView) convertView.findViewById(R.id.date_start_textView);
        Schedule schedule = list.get(i);
        content_textView.setText(schedule.getContent());
        String date = new BmobDate(new Date(Long.parseLong(schedule.getDate_start()))).getDate();
        String[] a = date.substring(0, date.length() - 3).split(" ");
        Log.i("danger", "time-->>" + date);
        date_start_textView.setText(a[0] + "    " + a[1]);
        if (schedule.getType().equals("工作")) {
            view.setBackgroundColor(work_color);
        } else if (schedule.getType().equals("行程")) {
            view.setBackgroundColor(route_color);
        } else if (schedule.getType().equals("生活")) {
            view.setBackgroundColor(life_color);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

}
